'use strict';
const moment = require('moment')

module.exports = async function factory (app, conf) {
    app.fs.socket.on('connection', async (ws, req) => {
        const addtime = moment()
        const ip = req?.headers['x-forwarded-for']?.split(',')[0]?.trim() ?? req?.socket?.remoteAddress;
        app.fs.logger.info(`来自 ${ip} 的 WS 客户端已连接`);
        ws.on('close', async (reason) => {
            app.fs.logger.info(`来自 ${ip} 的 WS 客户端已断开连接 连接时长：${moment.duration(moment().diff(addtime)).humanize()}`);
        })
    })
}
